home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 5777 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.7 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: accessing structures (newbie question)
  5. Date: Mon, 19 Feb 96 19:34:08 GMT
  6. Organization: none
  7. Message-ID: <824758448snz@genesis.demon.co.uk>
  8. References: <4g8gic$o6u@news1.sunbelt.net> <3128FA60.5227@metagen.co.uk>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <3128FA60.5227@metagen.co.uk> rws@metagen.co.uk "Rob Stenton" writes:
  15.  
  16. >dking@SunBelt.Net wrote:
  17. >> 
  18. >> Ok. having trouble accessing structure.
  19. >
  20. >There are basically two ways of accessing elements within a malloced 
  21. >buffer:
  22. >1. Treat the malloced buffer as an array (as you suggested).
  23. >2. Increment a local pointer by sizeof(picture) in loop iteration.
  24.  
  25. No, pointer arithmetic works in units of the object pointed to so to move
  26. onto the next element of an array you always add 1 to the corresponding
  27. pointer.
  28.  
  29. >The first method is preferable because the second method involves pointer 
  30. >arithmetic (always best avoided) which can cause problems on different 
  31. >memory architectures and assumes things about BYTE sizes (but usually 
  32. >works).
  33.  
  34. If you follow the rules pointer arithmetic is guaranteed to work. In fact
  35. array indexing is defined in terns pfo pointer arithmetic i.e. a[i] is
  36. defined to be evaluated as *((a)+(i))
  37.  
  38. >I think you may have been using the wrong syntax when you tried the array 
  39. >method. The xth item is given by 'photos[x].' which is equivalent to 
  40. >'(photos + x * sizeof(picture))->'.
  41.  
  42. It is equivalent to (photos + x)->
  43.  
  44. >The two ways for solving this problem are therefore:
  45. >1. (Recommended)
  46. >         for (x=0;x<num_of_files;x++)
  47. >         {
  48. >                 strncpy(photos[x].filename, filelist[x],12);
  49. >                 strncpy(photos[x].diskname, inputdisk,12);
  50. >                 strncpy(photos[x].indexname, inputindex,12);
  51. >         }
  52.  
  53. strncpy may not be what is required here (or in the original). It does
  54. *not* guarantee that what you end up with is a string (i.e. null terminated).
  55.  
  56. >or
  57. >2. (Not recommended)
  58. >         for (x=0;x<num_of_files;x++)
  59. >         {
  60. >                 strncpy((photos + x * sizeof(picture))->filename, 
  61. >filelist[x],12);
  62. >                 strncpy((photos + x * sizeof(picture))->diskname, 
  63. >inputdisk,12);
  64. >                 strncpy((photos + x * sizeof(picture))->indexname, 
  65. >inputindex,12);
  66. >         }
  67.  
  68. Remove the * sizeof(picture) and this is entirely equivalent to the first
  69. way (although I agree that the first way is clearer).
  70.  
  71. -- 
  72. -----------------------------------------
  73. Lawrence Kirby | fred@genesis.demon.co.uk
  74. Wilts, England | 70734.126@compuserve.com
  75. -----------------------------------------
  76.